{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 糖尿病预测-Logistics回归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pregnants_tfid</th>\n",
       "      <th>Plasma_glucose_concentration_tfid</th>\n",
       "      <th>blood_pressure_tfid</th>\n",
       "      <th>Triceps_skin_fold_thickness_tfid</th>\n",
       "      <th>serum_insulin_tfid</th>\n",
       "      <th>BMI_tfid</th>\n",
       "      <th>Diabetes_pedigree_function_tfid</th>\n",
       "      <th>Age_tfid</th>\n",
       "      <th>Target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>136</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5052450751524796</td>\n",
       "      <td>0.6564135178274744</td>\n",
       "      <td>0.2891853493803983</td>\n",
       "      <td>0.060509690920132995</td>\n",
       "      <td>0.3612742056067209</td>\n",
       "      <td>0.30990331882579925</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>543</td>\n",
       "      <td>0.29923405673060294</td>\n",
       "      <td>0.28430073208766715</td>\n",
       "      <td>0.7419378799124576</td>\n",
       "      <td>0.19184328108832382</td>\n",
       "      <td>0.05561291363854306</td>\n",
       "      <td>0.4811161526330725</td>\n",
       "      <td>0.03810200353715479</td>\n",
       "      <td>0.07961846342321281</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>564</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3790340168375279</td>\n",
       "      <td>0.7142890286907821</td>\n",
       "      <td>0.2993033894682619</td>\n",
       "      <td>0.1667676007490198</td>\n",
       "      <td>0.3639326519314327</td>\n",
       "      <td>0.2791430544445937</td>\n",
       "      <td>0.1355087671654874</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>532</td>\n",
       "      <td>0.07243282333604019</td>\n",
       "      <td>0.2890358159487157</td>\n",
       "      <td>0.457148484408683</td>\n",
       "      <td>0.5224244682904001</td>\n",
       "      <td>0.06538542024595347</td>\n",
       "      <td>0.5052039052268025</td>\n",
       "      <td>0.382128244937688</td>\n",
       "      <td>0.15418004644070327</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>618</td>\n",
       "      <td>0.4910203295083605</td>\n",
       "      <td>0.35247866450385007</td>\n",
       "      <td>0.4755076821563044</td>\n",
       "      <td>0.1486556660782656</td>\n",
       "      <td>0.10719024715053632</td>\n",
       "      <td>0.16473135445851783</td>\n",
       "      <td>0.4130429520376531</td>\n",
       "      <td>0.420976383872468</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          pregnants_tfid Plasma_glucose_concentration_tfid  \\\n",
       "136                  0.0                0.5052450751524796   \n",
       "543  0.29923405673060294               0.28430073208766715   \n",
       "564                  0.0                0.3790340168375279   \n",
       "532  0.07243282333604019                0.2890358159487157   \n",
       "618   0.4910203295083605               0.35247866450385007   \n",
       "\n",
       "    blood_pressure_tfid Triceps_skin_fold_thickness_tfid  \\\n",
       "136  0.6564135178274744               0.2891853493803983   \n",
       "543  0.7419378799124576              0.19184328108832382   \n",
       "564  0.7142890286907821               0.2993033894682619   \n",
       "532   0.457148484408683               0.5224244682904001   \n",
       "618  0.4755076821563044               0.1486556660782656   \n",
       "\n",
       "       serum_insulin_tfid             BMI_tfid  \\\n",
       "136  0.060509690920132995   0.3612742056067209   \n",
       "543   0.05561291363854306   0.4811161526330725   \n",
       "564    0.1667676007490198   0.3639326519314327   \n",
       "532   0.06538542024595347   0.5052039052268025   \n",
       "618   0.10719024715053632  0.16473135445851783   \n",
       "\n",
       "    Diabetes_pedigree_function_tfid             Age_tfid  Target  \n",
       "136             0.30990331882579925                  0.0       0  \n",
       "543             0.03810200353715479  0.07961846342321281       0  \n",
       "564              0.2791430544445937   0.1355087671654874       0  \n",
       "532               0.382128244937688  0.15418004644070327       0  \n",
       "618              0.4130429520376531    0.420976383872468       1  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读入特征工程后的ORG数据\n",
    "data1 = pd.read_csv('FE_pima-indians-diabetes.csv',decimal=';')\n",
    "data2 = pd.read_csv('FE_Tfid_pima-indians-diabetes.csv',decimal=';')\n",
    "data2.sample(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "准备特征和标签数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train1 = data1.drop(['Target'],axis=1)\n",
    "X_train2 = data2.drop(['Target'],axis=1)\n",
    "y_label = data1['Target']\n",
    "featrue_name_tfid = X_train2.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 默认参数的logistic回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "损失值：[0.49200504 0.52099888 0.47770562 0.45251079 0.4886484 ]，损失平均值：0.4863737457555152\n"
     ]
    }
   ],
   "source": [
    "# 原始数据\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import cross_val_score\n",
    "log_r = LogisticRegression(solver='liblinear')\n",
    "loss1 = cross_val_score(log_r,X_train1,y_label,cv=5,scoring='neg_log_loss')\n",
    "print('损失值：{}，损失平均值：{}'.format(-loss1,-loss1.mean()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "损失值：[0.56122106 0.57490417 0.5594425  0.55721138 0.54433901]，损失平均值：0.5594236230143441\n"
     ]
    }
   ],
   "source": [
    "# tfid后的数据\n",
    "loss2 = cross_val_score(log_r,X_train2,y_label,cv=5,scoring='neg_log_loss')\n",
    "print('损失值：{}，损失平均值：{}'.format(-loss2,-loss2.mean()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 正则化的 Logistic Regression及参数调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.4755664138749241 {'C': 10, 'penalty': 'l2'}\n"
     ]
    }
   ],
   "source": [
    "# 原始数据\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "penalty = ['l1','l2']\n",
    "C = [0.001,0.01,0.1,1,10,100,1000]\n",
    "param = dict(penalty=penalty,C=C)\n",
    "lr = LogisticRegression(solver='liblinear')\n",
    "grid = GridSearchCV(lr,param,cv=5,scoring='neg_log_loss')\n",
    "grid.fit(X_train1,y_label)\n",
    "print(grid.best_score_,grid.best_params_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 发现做完参数调优比没做降低了1%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.535808485814417 {'C': 100, 'penalty': 'l2'}\n"
     ]
    }
   ],
   "source": [
    "# Tfid后的数据\n",
    "solver = ['newton-cg','lbfgs','liblinear','sag']\n",
    "lr_tfid = LogisticRegression(solver='liblinear')\n",
    "grid_tfid = GridSearchCV(lr,cv=5,param_grid=param,scoring='neg_log_loss')\n",
    "grid_tfid.fit(X_train2,y_label)\n",
    "print(grid_tfid.best_score_,grid_tfid.best_params_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 发现没有做tfid的数据比做了tfid的数据低"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
